package com.xhtt.modules.app.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.R;
import com.xhtt.common.validator.ValidatorUtils;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.sys.controller.AbstractController;
import com.xhtt.modules.sys.entity.SysDeptEntity;
import com.xhtt.modules.sys.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 部门管理
 */
@Api(value = "系统管理-部门管理", tags = "系统管理-部门管理")
@RestController
@RequestMapping("app/sys/dept")
public class SysDeptController extends AbstractController {
    @Autowired
    private SysDeptService sysDeptService;


    /**
     * 部门树
     */
    @ApiOperation("系统管理-部门管理-部门树(web/app)withoutTeam设值表示结果剔除组；needUser设值表示要查询部门内人员")
    @GetMapping("/tree")
    @Login
    public R tree(@RequestParam Map<String, Object> params) {
        List<SysDeptEntity> list = sysDeptService.tree(params);
        return R.ok().put(Constant.DATA, list);
    }


    /**
     * 新增部门或组
     */
    @ApiOperation("新增部门或组(web)")
    @PostMapping("/save")
    @Login
    public R save(@RequestBody SysDeptEntity dept) {
        ValidatorUtils.validateEntity(dept);
        if (dept.getType() == 1) {
            int count = sysDeptService.count(new LambdaQueryWrapper<SysDeptEntity>().eq(SysDeptEntity::getType, 1).eq(SysDeptEntity::getName, dept.getName()));
            if (count > 0) {
                throw new RRException("对象已存在");
            }
        } else {
            int count = sysDeptService.count(new LambdaQueryWrapper<SysDeptEntity>().eq(SysDeptEntity::getType, 2).eq(SysDeptEntity::getParentId, dept.getParentId()).eq(SysDeptEntity::getName, dept.getName()));
            if (count > 0) {
                throw new RRException("对象已存在");
            }
        }

        sysDeptService.save(dept);
        return R.ok();
    }

    /**
     * 获取详情
     */
    @ApiOperation("新增部门或组(web/app)")
    @GetMapping("/info/{deptId}")
    @Login
    public R info(@PathVariable("deptId") Integer deptId) {
        SysDeptEntity dept = sysDeptService.getById(deptId);
        return R.ok().put(Constant.DATA, dept);
    }

    /**
     * 修改部门或组
     */
    @ApiOperation("修改部门或组(web)")
    @PostMapping("/update")
    @Login
    public R update(@RequestBody SysDeptEntity dept) {
        Integer deptId = dept.getDeptId();
        String name = dept.getName();
        if (null == deptId || StringUtils.isEmpty(name)) {
            throw new RRException("参数有误");
        }

        if (dept.getType() == 1) {
            int count = sysDeptService.count(new LambdaQueryWrapper<SysDeptEntity>().eq(SysDeptEntity::getType, 1)
                    .eq(SysDeptEntity::getName, dept.getName()).ne(SysDeptEntity::getDeptId, deptId));
            if (count > 0) {
                throw new RRException("对象已存在");
            }
        } else {
            int count = sysDeptService.count(new LambdaQueryWrapper<SysDeptEntity>().eq(SysDeptEntity::getType, 2).eq(SysDeptEntity::getParentId, dept.getParentId())
                    .eq(SysDeptEntity::getName, dept.getName()).ne(SysDeptEntity::getDeptId, deptId));
            if (count > 0) {
                throw new RRException("对象已存在");
            }
        }

        SysDeptEntity nd = new SysDeptEntity();
        nd.setName(name);
        sysDeptService.update(nd, new QueryWrapper<SysDeptEntity>().eq(SysDeptEntity.DEPTID, deptId));
        return R.ok();
    }

    /**
     * 删除部门或组
     */
    @ApiOperation("删除部门或组(web)")
    @PostMapping("/delete")
    @Login
    public R delete(@RequestBody Integer[] deptIds) {
        sysDeptService.delete(deptIds[0]);
        return R.ok();
    }

    /**
     * 根据部门获取组
     */
    @ApiOperation("根据部门获取组(web/app)")
    @GetMapping("/teamByDept")
    @Login
    public R teamByDept(@RequestParam Integer deptId) {
        List<SysDeptEntity> list = sysDeptService.teamByDept(deptId);
        return R.ok().put(Constant.DATA, list);
    }

    /**
     * 获取所有组
     *
     * @return
     */
    @GetMapping(value = "/teamList")
    @ApiOperation("获取所有组集合(web/app)")
    public R teamList() {
        return R.ok().put(Constant.DATA, sysDeptService.teamList());
    }
}
